package com.zoltu.GoogleAuthenticator;

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ActiveAuthenticationsActivity extends Activity implements Handler.Callback
{
	private ListView mListView;
	private ActiveAuthenticationsAdapter mListAdapter;
	private Handler mHandler;
	
	@Override protected void onCreate(Bundle pSavedInstanceState)
	{
		super.onCreate(pSavedInstanceState);
		
		/* Set the layout file to use for this Activity. */
		setContentView(R.layout.active_authentications_activity);
		
		/* Setup the handler for this Activity's message queue. */
		mHandler = new Handler(this);
		
		/* Get handles to the various views in the Activity. */
		mListView = (ListView) findViewById(R.id.authenticators_list);
		
		/* Create an adapter for getting the content of the list. */
		mListAdapter = new ActiveAuthenticationsAdapter();
		
		/* Set the content of the list view to that of the adapter. */
		mListView.setAdapter(mListAdapter);
	}
	
	@Override protected void onResume()
	{
		super.onResume();
		
		/*
		 * Send an intent over to AuthenticatorService to register mHandler as a receiver of thread change
		 * notifications.
		 */
		Messenger lMessenger = new Messenger(mHandler);
		Intent lIntent = new Intent(this, AuthenticatorService.class);
		lIntent.setAction(AuthenticatorService.ACTION_REGISTER_AUTHENTICATIONS_LISTENER);
		lIntent.putExtra(AuthenticatorService.KEY_MESSENGER, lMessenger);
		startService(lIntent);
	}
	
	@Override protected void onPause()
	{
		super.onPause();
		
		/* Unregister mHandler from AuthenticatorService when the activity loses focus. */
		Messenger lMessenger = new Messenger(mHandler);
		Intent lIntent = new Intent(this, AuthenticatorService.class);
		lIntent.setAction(AuthenticatorService.ACTION_UNREGISTER_AUTHENTICATIONS_LISTENER);
		lIntent.putExtra(AuthenticatorService.KEY_MESSENGER, lMessenger);
		startService(lIntent);
	}
	
	@Override public boolean handleMessage(Message pMessage)
	{
		/*
		 * TODO: Receive notification about any changes to the active authentications and pass those changes along to
		 * the ActiveAuthenticationsAdapter so they can be updated in the UI.
		 */
		return false;
	}
	
	private class ActiveAuthenticationsAdapter implements ListAdapter
	{
		public ActiveAuthenticationsAdapter()
		{
			
		}
		
		@Override public boolean areAllItemsEnabled()
		{
			return false;
		}
		
		@Override public boolean isEnabled(int pPosition)
		{
			return true;
		}
		
		@Override public int getCount()
		{
			return 0;
		}
		
		@Override public Object getItem(int pPosition)
		{
			return null;
		}
		
		@Override public long getItemId(int pPosition)
		{
			return 0;
		}
		
		@Override public int getItemViewType(int pPosition)
		{
			return 0;
		}
		
		@Override public View getView(int pPosition, View pConvertView, ViewGroup pParent)
		{
			return null;
		}
		
		@Override public int getViewTypeCount()
		{
			return 0;
		}
		
		@Override public boolean hasStableIds()
		{
			return true;
		}
		
		@Override public boolean isEmpty()
		{
			return false;
		}
		
		@Override public void registerDataSetObserver(DataSetObserver pObserver)
		{
			
		}
		
		@Override public void unregisterDataSetObserver(DataSetObserver pObserver)
		{
			
		}
	}
}
